// 模版公共_API
import httpService from '@/api/httpService';
import { base } from '@/config/api-url-config';

// ================================================== 获取用户信息
const GET_USERINFO_API = function () {
  return new Promise((resolve, reject) => {
    httpService
      .accessAPI({
        apiObj: {
          url: `${base}/getUserInfo`,
          method: 'GET'
        }
      })
      .then(res => {
        // 对请求该接口数据，进行公共 验证或处理
        if (res.status) {
          resolve(res);
        } else {
          reject(res);
        }
      })
      .catch(error => {
        reject(error);
      });
  });
};
// ================================================== 获取供应商下拉
const GET_SUPPLIER_CODE_BY_DEVICE_MODEL = function (body) {
  return new Promise((resolve, reject) => {
    httpService
      .accessAPI({
        apiObj: { url: `${base}/getSupplierByequipmentModel`, method: 'GET' },
        body
      })
      .then(res => {
        // 对请求该接口数据，进行公共 验证或处理
        if (res.status) {
          resolve(res);
        } else {
          reject(res);
        }
      })
      .catch(error => {
        reject(error);
      });
  });
};

// ================================================== 退出 接口
// const GET_LOGINOUT_API = function () {
//   return new Promise(resolve => {
//     window.open( , '_self');
//     resolve(true);
//   });
// };
const GET_LOGINOUT_API = function (body) {
  httpService
    .accessAPI({
      apiObj: { url: `${base}/ssologout`, method: 'GET' },
      body
    })
    .then(res => {
      if (res.status) {
        window.open(res.result, '_self');
      };
    });
};

// ================================================== 清楚 公共 SESSEION
const DELETE_SESSEION_API = function () {
  return new Promise((resolve, reject) => {
    httpService
      .accessAPI({
        params: {
          url: `https://connect.nano.com/session`,
          method: 'DELETE'
        }
      })
      .then(res => {
        if (res.status) {
          resolve(res);
        } else {
          reject(res);
        }
      })
      .catch(error => {
        reject(error);
      });
  });
};

// ================================================== 导出为Excel
const EXPORT_ALL = function (list, allColumns, name) {
  var currentTime = new Date(new Date().getTime()).Format('yyyy-MM-dd hh:mm:ss');
  var columnNames = [];
  var columnValues = [];
  for (var i = 0; i < allColumns.length; i++) {
    columnNames[i] = allColumns[i].label;
    columnValues[i] = allColumns[i].prop;
  }
  require.ensure([], () => {
    const { exportJsonToExcel } = require('vendor/Export2Excel');
    const eHeader = columnNames;
    const filterVal = columnValues;
    const data = formatJson(filterVal, list);
    exportJsonToExcel(eHeader, data, `${name}_${currentTime}`);
  });
};
const formatJson = function (filterVal, list) {
  return list.map(v => filterVal.map(j => v[j]));
};
// ================================================== 全局页面获取二级字典列表(parentId: 一级菜单id(integer))
const GET_SECOND_DICTIONARY_LIST = function (parentId) {
  const body = {
    parentId: parentId
  };
  return new Promise((resolve) => {
    httpService
      .accessAPI({
        apiObj: { url: `${base}/getSecondDatadictionaryInfo`, method: 'GET' },
        body
      })
      .then(res => {
        // 对请求该接口数据，进行公共 验证或处理
        resolve(res);
      });
  });
};
export {
  GET_USERINFO_API, // 获取 用户信息 URL
  GET_LOGINOUT_API, // 请求 退出 当前用户
  DELETE_SESSEION_API, // 清除 公共 sesseion
  EXPORT_ALL, // 导出为Excel
  GET_SECOND_DICTIONARY_LIST, // 获取二级字典列表
  GET_SUPPLIER_CODE_BY_DEVICE_MODEL // 获取供应商下拉
};
